package com.ids.enigha;

import java.io.File;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.util.Log;

public class DatabaseHelper
{
	public final static String DBTABLE = "SampleTable";
	public final static String deviceId = "DEVICE_ID";
	public final static String userName = "USER_NAME";
	public final static String latitude = "LATITUDE";
	public final static String longitude = "LONGITUDE";
	public final static String timeStamp = "TIME_STAMP";
	public final static String batterylevel = "BATTERY_LEVEL";
	private static final String TAG = "DatabaseHelper";
	public static final String DATABASE_FILE_PATH = "mnt/sdcard";
	public static final String DATABASE_NAME = "GpsTrack";

	public final static String TRACKS_TABLE_CREATE = "CREATE TABLE IF NOT EXISTS SampleTable(_id integer PRIMARY KEY AUTOINCREMENT,DEVICE_ID text,USER_NAME text,LATITUDE text,LONGITUDE text,TIME_STAMP text,BATTERY_LEVEL text,Accuracy text)";
	private SQLiteDatabase database;

	public DatabaseHelper() {

			try {

				database = SQLiteDatabase.openOrCreateDatabase(DATABASE_FILE_PATH
						+ File.separator + DATABASE_NAME, null);
				createTables();
			}

			catch (SQLiteException ex) {
				// createTables();
				Log.e(TAG, "error -- " + ex.getMessage(), ex);
			}

	}

	private void createTables() {
		try {
			database.execSQL(TRACKS_TABLE_CREATE);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public SQLiteDatabase getReadableDatabase() {
		database = SQLiteDatabase.openDatabase(DATABASE_FILE_PATH

		+ File.separator + DATABASE_NAME, null,

		SQLiteDatabase.OPEN_READONLY);
		return database;
	}

	public SQLiteDatabase getWritableDatabase() {
		database = SQLiteDatabase.openDatabase(DATABASE_FILE_PATH

		+ File.separator + DATABASE_NAME, null,

		SQLiteDatabase.OPEN_READWRITE);

		return database;
	}

	public long insertIntoDatabase(String devId, String uName, String lat,
			String lon, String tStamp, String blevel, String acc) {
		long result = 0;
		ContentValues cValues = new ContentValues();
		cValues.put(deviceId, devId);
		cValues.put(userName, uName);
		cValues.put(latitude, lat);
		cValues.put(longitude, lon);
		cValues.put(timeStamp, tStamp);
		cValues.put(batterylevel, blevel);
		cValues.put("Accuracy", acc);

		result = database.insert(DBTABLE, null, cValues);

		Log.d("insert result", "" + result);
		// result = db.insertOrThrow(DBTABLE, null, cValues);
		return result;
	}

	public void deleteData(String devId) {
		String where = "TIME_STAMP = ?";
		String params[] = { devId };
		database.delete(DBTABLE, where, params);
	}
	public void deleteEntireDatabase()
	{
		database.delete(DBTABLE, null, null);
	}

	public Cursor display() {
		return database.query(DBTABLE, null, null, null, null, null, null);
	}

	public void close() {
		try {
			if (database != null && database.isOpen()) {
				database.close();
				database = null;
			}
		} catch (Exception e) {
		}
	}

}